package oldFiles;
import java.util.ArrayList;

public class Table {

	private ArrayList<Player> players = new ArrayList<Player>(4);
	private Integer number = new Integer(0);

	Table() {
	}

	Table(ArrayList<Player> players) throws Exception {
		setPlayers(players);
	}

	Table(Player player1, Player player2, Player player3, Player player4)
			throws Exception {
		ArrayList<Player> temp = new ArrayList<Player>(4);
		temp.add(player1);
		temp.add(player2);
		temp.add(player3);
		temp.add(player4);
		setPlayers(temp);
	}

	Player getRandomPlayer() {
		//TODO дописать метод
		return null;
	}
	
	void addPlayer(Player player) throws Exception {
		if (players.size() >= 4) {
			throw new Exception("Стол переполнен.");
		}
		for (int i = 0; i < players.size(); i++) {
			if (players.get(i).getFull_name().equals(player.getFull_name())) {
				throw new Exception("Игрок уже есть за данным столом.");
			}
		}
		players.add(player);
	}

	public Integer getNumber() {
		return number;
	}

	public void setNumber(Integer number) throws Exception {
		if (number < 0) {
			throw new Exception("Номер стола не может быть отрицательным числом.");
		}
		this.number = number;
	}

	public void setPlayers(ArrayList<Player> players) throws Exception {
		if (players.size() != 4) {
			throw new Exception("Число игроков за столом должно быть равно четырем.");
		}
		if (!isUniq(players)) {
			throw new Exception("Совпадающие имена игроков за одним столом.");
		}
		setPlayers(players);
	}

	public ArrayList<Player> getPlayers() {
		return players;
	}

	private Boolean isUniq(ArrayList<Player> players) {
		for (int i = 0; i < players.size(); i++) {
			for (int j = 1; j < players.size(); j++) {
				if (players.get(i).getFull_name()
						.equals(players.get(j).getFull_name())) {
					return false;
				}
			}
		}
		return true;
	}
}
